package aaa.brush.service.impl;

import aaa.brush.domain.BruOnlineBrushRecord;
import aaa.brush.domain.OptionBank;
import aaa.brush.domain.QuestionBank;
import aaa.brush.domain.QuestionDTO;
import aaa.brush.mapper.BruOnlineBrushRecordMapper;
import aaa.brush.mapper.OptionBankMapper;
import aaa.brush.mapper.QuestionBankMapper;
import aaa.brush.service.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class IQuestionServiceImpl implements IQuestionService {

    @Autowired
    private BruOnlineBrushRecordMapper bruOnlineBrushRecordMapper;

    @Autowired
    private QuestionBankMapper questionBankMapper;

    @Autowired
    private OptionBankMapper optionBankMapper;

    @Override
    public QuestionDTO getNextQuestion(int userId, String questionType) {
        Boolean isLastQuestion = false;//判断是否为当前类别最后一道题标识
        int maxTypeQuestionNum ;//当前类别最后一道题的类别题号
        //获取类别最后一道题的类别题号
        switch (questionType) {
            case "java":
                maxTypeQuestionNum = questionBankMapper.findMaxJavaQuestionNumByQuestionType(questionType);
                break;
            case "c":
                maxTypeQuestionNum = questionBankMapper.findMaxCQuestionNumByQuestionType(questionType);
                break;
            case "computer":
                maxTypeQuestionNum = questionBankMapper.findMaxComputerQuestionNumByQuestionType(questionType);
                break;
            default:
                throw new RuntimeException("Unsupported question type: " + questionType);
        }
        //根据用户传入的userId和questionType获取用户上一次刷题记录 records
        List<BruOnlineBrushRecord> records = bruOnlineBrushRecordMapper.findByUserIdAndQuestionType(userId, questionType);
        int brushId;//当前在线刷题需要刷的类别题号

        //如果无上一次刷题记录则 当前在线刷题需要刷的类别题号为1
        if (records.isEmpty()) {
            brushId = 1;
        } else {//如果有上一次刷题记录则 当前在线刷题需要刷的类别题号 通过上一次刷题记录的最大题号对应的类别题号加1

            //通过stream流获取上一次刷题记录的最大题号
            Integer maxQuestionId = records.stream()
                    .map(BruOnlineBrushRecord::getQuestionId)
                    .filter(Objects::nonNull)  // 添加这一步来过滤掉null值
                    .max(Integer::compareTo)
                    .orElse(0);


            //根据上一次刷题记录的最大题号获取当前在线刷题需要刷的类别题号
            switch (questionType) {
                case "java":
                    brushId = questionBankMapper.findJavaIdByQuestionId(maxQuestionId);
                    break;
                case "c":
                    brushId = questionBankMapper.findCIdByQuestionId(maxQuestionId);
                    break;
                case "computer":
                    brushId = questionBankMapper.findComputerIdByQuestionId(maxQuestionId);
                    break;
                default:
                    throw new RuntimeException("Unsupported question type: " + questionType);
            }
            //判断上一次刷题记录是否为当前类别最后一道题
            if(brushId == maxTypeQuestionNum){
                isLastQuestion = true;
            }

            if (isLastQuestion) {
                // 如果已完成当前类别的所有题目，返回提示信息
                return new QuestionDTO() {
                    {
                        setMessage("您已完成整个" + questionType + "的题库的在线刷题。");
                    }
                };
            }

            //根据上一次刷题记录的最大题号获取当前在线刷题需要刷的类别题号
            switch (questionType) {
                case "java":
                    brushId = questionBankMapper.findJavaIdByQuestionId(maxQuestionId)+1;
                    break;
                case "c":
                    brushId = questionBankMapper.findCIdByQuestionId(maxQuestionId)+1;
                    break;
                case "computer":
                    brushId = questionBankMapper.findComputerIdByQuestionId(maxQuestionId)+1;
                    break;
                default:
                    throw new RuntimeException("Unsupported question type: " + questionType);
            }


        }

        QuestionBank questionBank;
        switch (questionType) {
            case "java":
                questionBank = questionBankMapper.findByQuestionTypeAndQuestionIdJava(questionType,brushId);
                break;
            case "c":
                questionBank = questionBankMapper.findByQuestionTypeAndQuestionIdC(questionType,brushId);
                break;
            case "computer":
                questionBank = questionBankMapper.findByQuestionTypeAndQuestionIdComputer(questionType,brushId);
                break;
            default:
                throw new RuntimeException("Unsupported question type: " + questionType);
        }

        if (questionBank == null) {
            throw new RuntimeException("No questions found for the given type and ID");
        }
        if (questionBank != null) {
            // 打印 QuestionBank 对象的详细信息
            System.out.println("Question Type: " + questionBank.getQuestionType());
            System.out.println("Question ID: " + questionBank.getQuestionId());
            System.out.println("Question Text: " + questionBank.getQuestionText());
            System.out.println("Correct Answer: " + questionBank.getCorrectAnswer());
        }

        List<OptionBank> options = optionBankMapper.findByQuestionId(questionBank.getQuestionId());

        for (OptionBank option : options) {
            System.out.println("ID: " + option.getId());
            System.out.println("Question ID: " + option.getQuestionId());
            System.out.println("Option Character: " + option.getOptionChar());
            System.out.println("Option Text: " + option.getOptionText());
            System.out.println("-------------------------");
        }

        QuestionDTO questionDTO = new QuestionDTO();
        questionDTO.setQuestionId(questionBank.getQuestionId());
        questionDTO.setQuestionText(questionBank.getQuestionText());
        questionDTO.setCorrectAnswer(questionBank.getCorrectAnswer());

        Map<Character, String> optionMap = options.stream()
                .collect(Collectors.toMap(OptionBank::getOptionChar, OptionBank::getOptionText));
        questionDTO.setOptionTextA(optionMap.getOrDefault('A', ""));
        questionDTO.setOptionTextB(optionMap.getOrDefault('B', ""));
        questionDTO.setOptionTextC(optionMap.getOrDefault('C', ""));
        questionDTO.setOptionTextD(optionMap.getOrDefault('D', ""));


        return questionDTO;
    }

    @Override
    public int submit(BruOnlineBrushRecord record) {
        return bruOnlineBrushRecordMapper.insertRecord(record);
    }
}